package canss.webservice.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import canss.supports.basic;
import canss.webservice.model.ContentDesc;
import canss.webservice.model.ContentsDBContent;
import canss.webservice.model.EventDescription;
import canss.webservice.service.PortalIntraWebServiceControl;
import canss.webservice.service.PortalIntraWebServiceControlBean;


public class svtTRAdminDesc extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet
{
	private static final long serialVersionUID = -6760110842083635642L;
	PortalIntraWebServiceControl control; // control = (control==null)?new
											// PortalIntraWebServiceControlBean():control;

	public svtTRAdminDesc()
	{
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{

		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();

		String _pid = request.getParameter("_pid");
		String _notxml = request.getParameter("_notxml");
		if (_notxml != null)
		{// in html
		} else
		{// in xml
			out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
			weblogic.application.utils.XMLWriter writer = new weblogic.application.utils.XMLWriter(out);

			writer.addElement("canss");
			writer.addElement("canssContent_mediaGroup");
			if (!basic.isEmpty(_pid))
			{
				control = (control == null) ? new PortalIntraWebServiceControlBean() : control;
				ContentDesc[] contentDescByPID = control.getContentDescByPID(_pid);

				if (contentDescByPID != null)
				{
					Hashtable<String, ContentsDBContent> contentlist_hash = getContentList_hash(control, contentDescByPID);
					//
					for (ContentDesc desc : contentDescByPID)
					{
						ContentsDBContent curcontent = contentlist_hash.get(desc.getTBLCID_CID());
						
						writer.addElement("canssContent_media");
						writer.addElement("dc_identifier", (curcontent != null ? curcontent.getDC_IDENTIFIER() : "null"));
						writer.addElement("xmp_Identifier", (curcontent != null ? curcontent.getXMP_IDENTIFIER() : ""));
						writer.addElement("dc_title", (curcontent != null ? curcontent.getDC_TITLE() : ""));
						writer.addElement("dc_description ", (curcontent != null ? curcontent.getDC_DESCRIPTION() : ""));
						writer.addElement("canssContent_Thumbnail ", (curcontent != null ? curcontent.getCANSSCONTENT_THUMBNAIL(): ""));
						writer.addElement("canssContentDescription_contentId", desc.getTBLCID_CID());
						writer.addElement("canssContentDescription_views", basic.var2str(desc.getCANSSCONTENTDESCRIPTION_VIEWS()));
						writer.addElement("canssContentDescription_rating", basic.var2str(desc.getCANSSCONTENTDESCRIPTION_RATING()));
						writer.addElement("canssContentDescription_owner", (curcontent != null ? curcontent.getCANSSCONTENTDESCRIPTION_OWNER() : ""));
						writer.addElement("dc_publisher", (curcontent != null ? curcontent.getDC_PUBLISHER() : ""));
						writer.addElement("canssContentDescription_price");
						writer.addAttribute("currencyCode", basic.var2str(desc.getCANSSCONTENTDESCRIPTION_PRICEATT_CURRENCYCODE()));
						writer.addText(basic.var2str(desc.getCANSSCONTENTDESCRIPTION_PRICEVALUE()));
						writer.closeElement();
						writer.addElement("canssContentDescription_status", basic.var2str(desc.getCANSSCONTENTDESCRIPTION_STATUS()));
						EventDescription retevent = control.getEventDescByEventID(desc.getCANSSCONTENTDESCRIPTION_STATUS());
						if (retevent != null && retevent.getONELINEDESC() != null)
						{
							writer.addElement("canssContentDescription_statusdesc", retevent.getONELINEDESC());
						}
						writer.addElement("xmpdm_Moddate", curcontent.getXMPDM_MODDATE());
						writer.addElement("js_submit", "$(this).parent().ajaxSubmit({target:'#div_{$v_cid}'});".replace("{$v_cid}", desc.getTBLCID_CID()));
						writer.closeElement();
					}
				}
			}
			writer.finish();
		}
	}
	
	private static Hashtable<String, ContentsDBContent> getContentList_hash(PortalIntraWebServiceControl _control, ContentDesc[] _contentDescByPID)
	{
		Hashtable<String, ContentsDBContent> contentlist_hash = new Hashtable<String, ContentsDBContent>();
		{
			String query = "";
			boolean isfirst = true;
			for (ContentDesc desc : _contentDescByPID)
			{
				if (isfirst)
					isfirst = false;
				else
					query += " OR ";
				query += "tblCID_cid=" + "\"" + desc.getTBLCID_CID() + "\"";
			}

			ContentsDBContent[] retcontentlist = _control.getContentsByFilter(query);
			// if(true)throw new RuntimeException("adsf"+contentlist+query);

			if (retcontentlist != null)
				for (ContentsDBContent content : retcontentlist)
					contentlist_hash.put(content.getCANSSCONTENTDESCRIPTION_CONTENTID(), content);

		}
		return contentlist_hash;
	}
}